home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / gadtools / ListView.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  10.3 KB  |  319 lines

  1. /*
  2. COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992-1999
  3. Amiga, Inc.  All rights reserved.
  4.  
  5. DISCLAIMER: This software is provided "as is".  No representations or
  6. warranties are made with respect to the accuracy, reliability, performance,
  7. currentness, or operation of this software, and all use is at your own risk.
  8. Neither Amiga nor the authors assume any responsibility or liability
  9. whatsoever with respect to your use of this software.
  10. */
  11.  
  12. /* listview.c - a simple program showing how to use listview rendering
  13.  * callback functions
  14.  */
  15.  
  16.  
  17. /****************************************************************************/
  18.  
  19.  
  20. #include <exec/types.h>
  21. #include <intuition/intuition.h>
  22. #include <intuition/gadgetclass.h>
  23. #include <utility/hooks.h>
  24. #include <libraries/gadtools.h>
  25. #include <graphics/gfxmacros.h>
  26. #include <string.h>
  27. #include <dos.h>
  28.  
  29. #include <clib/exec_protos.h>
  30. #include <clib/intuition_protos.h>
  31. #include <clib/alib_protos.h>
  32. #include <clib/gadtools_protos.h>
  33. #include <clib/graphics_protos.h>
  34.  
  35. #include <pragmas/exec_pragmas.h>
  36. #include <pragmas/intuition_pragmas.h>
  37. #include <pragmas/gadtools_pragmas.h>
  38. #include <pragmas/graphics_pragmas.h>
  39.  
  40.  
  41. /****************************************************************************/
  42.  
  43.  
  44. static struct TextAttr topazAttr =
  45. {
  46.     "topaz.font",
  47.      8,
  48.      FS_NORMAL,
  49.      FPF_ROMFONT|FPF_DESIGNED
  50. };
  51.  
  52.  
  53. /*****************************************************************************/
  54.  
  55.  
  56. extern struct Library *SysBase;
  57. struct Library        *GfxBase;
  58. struct Library        *IntuitionBase;
  59. struct Library        *GadToolsBase;
  60. struct Library        *UtilityBase;
  61.  
  62. struct Screen         *screen;
  63. struct Window         *window;
  64. struct DrawInfo       *drawInfo;
  65. APTR                   visualInfo;
  66.  
  67.  
  68. /*****************************************************************************/
  69.  
  70.  
  71. static UWORD GhostPattern[2] =
  72. {
  73.      0x4444,
  74.      0x1111,
  75. };
  76.  
  77.  
  78. /* Apply a ghosting pattern to a given rectangle in a rastport */
  79. static VOID Ghost(struct RastPort *rp, UWORD pen, UWORD x0, UWORD y0, UWORD x1, UWORD y1)
  80. {
  81.     SetABPenDrMd(rp,pen,0,JAM1);
  82.     SetAfPt(rp,GhostPattern,1);
  83.     RectFill(rp,x0,y0,x1,y1);
  84.     SetAfPt(rp,NULL,0);
  85. }
  86.  
  87.  
  88. /*****************************************************************************/
  89.  
  90.  
  91. /* Erase any part of "oldExtent" which is not covered by "newExtent" */
  92. VOID FillOldExtent(struct RastPort *rp,
  93.                    struct Rectangle *oldExtent,
  94.                    struct Rectangle *newExtent)
  95. {
  96.     if (oldExtent->MinX < newExtent->MinX)
  97.         RectFill(rp,oldExtent->MinX,
  98.                     oldExtent->MinY,
  99.                     newExtent->MinX-1,
  100.                     oldExtent->MaxY);
  101.  
  102.     if (oldExtent->MaxX > newExtent->MaxX)
  103.         RectFill(rp,newExtent->MaxX+1,
  104.                     oldExtent->MinY,
  105.                     oldExtent->MaxX,
  106.                     oldExtent->MaxY);
  107.  
  108.     if (oldExtent->MaxY > newExtent->MaxY)
  109.         RectFill(rp,oldExtent->MinX,
  110.                     newExtent->MaxY+1,
  111.                     oldExtent->MaxX,
  112.                     oldExtent->MaxY);
  113.  
  114.     if (oldExtent->MinY < newExtent->MinY)
  115.         RectFill(rp,oldExtent->MinX,
  116.                     oldExtent->MinY,
  117.                     oldExtent->MaxX,
  118.                     newExtent->MinY-1);
  119. }
  120.  
  121.  
  122. /*****************************************************************************/
  123.  
  124.  
  125. /* This function is called whenever an item of the listview needs to be drawn
  126.  * by gadtools. The function must fill every pixel of the area described in
  127.  * the LVDrawMsg structure. This function does the exact same rendering as
  128.  * the built-in rendering function in gadtools, except that it render the
  129.  * normal items using the highlight text pen instead of simply text pen.
  130.  */
  131. static ULONG __asm RenderHook(register __a1 struct LVDrawMsg *msg,
  132.                               register __a2 struct Node *node)
  133. {
  134. struct RastPort   *rp;
  135. UBYTE              state;
  136. struct TextExtent  extent;
  137. ULONG              fit;
  138. WORD               x,y;
  139. WORD               slack;
  140. ULONG              apen;
  141. ULONG              bpen;
  142. UWORD             *pens;
  143. STRPTR             name;
  144.  
  145.     geta4();
  146.  
  147.     if (msg->lvdm_MethodID != LV_DRAW)
  148.         return(LVCB_UNKNOWN);
  149.  
  150.     rp    = msg->lvdm_RastPort;
  151.     state = msg->lvdm_State;
  152.     pens  = msg->lvdm_DrawInfo->dri_Pens;
  153.  
  154.     apen = pens[FILLTEXTPEN];
  155.     bpen = pens[FILLPEN];
  156.     if ((state == LVR_NORMAL) || (state == LVR_NORMALDISABLED))
  157.     {
  158.         apen = pens[HIGHLIGHTTEXTPEN];    /* this is normally TEXTPEN */
  159.         bpen = pens[BACKGROUNDPEN];
  160.     }
  161.     SetABPenDrMd(rp,apen,bpen,JAM2);
  162.  
  163.     name = node->ln_Name;
  164.  
  165.     fit = TextFit(rp,name,strlen(name),&extent,NULL,1,
  166.                   msg->lvdm_Bounds.MaxX-msg->lvdm_Bounds.MinX-3,
  167.                   msg->lvdm_Bounds.MaxY-msg->lvdm_Bounds.MinY+1);
  168.  
  169.     slack = (msg->lvdm_Bounds.MaxY - msg->lvdm_Bounds.MinY) - (extent.te_Extent.MaxY - extent.te_Extent.MinY);
  170.  
  171.     x = msg->lvdm_Bounds.MinX - extent.te_Extent.MinX + 2;
  172.     y = msg->lvdm_Bounds.MinY - extent.te_Extent.MinY + ((slack+1) / 2);
  173.  
  174.     extent.te_Extent.MinX += x;
  175.     extent.te_Extent.MaxX += x;
  176.     extent.te_Extent.MinY += y;
  177.     extent.te_Extent.MaxY += y;
  178.  
  179.     Move(rp,x,y);
  180.     Text(rp,name,fit);
  181.  
  182.     SetAPen(rp,bpen);
  183.     FillOldExtent(rp,&msg->lvdm_Bounds,&extent.te_Extent);
  184.  
  185.     if ((state == LVR_NORMALDISABLED) || (state == LVR_SELECTEDDISABLED))
  186.     {
  187.         Ghost(rp,pens[BLOCKPEN],msg->lvdm_Bounds.MinX, msg->lvdm_Bounds.MinY,
  188.                                 msg->lvdm_Bounds.MaxX, msg->lvdm_Bounds.MaxY);
  189.     }
  190.  
  191.     return(LVCB_OK);
  192. }
  193.  
  194.  
  195. /*****************************************************************************/
  196.  
  197.  
  198. /* Main entry point. Init the universe, and do the event loop */
  199. LONG main(VOID)
  200. {
  201. BOOL                 quit;
  202. struct IntuiMessage *intuiMsg;
  203. struct Gadget       *gadgetList;
  204. struct Hook          renderHook;
  205. struct List          list;
  206. struct Node          nodes[5];
  207. struct NewGadget     ng;
  208.  
  209.     renderHook.h_Entry = (HOOKFUNC)RenderHook;
  210.  
  211.     nodes[0].ln_Name = "First Node";
  212.     nodes[1].ln_Name = "Second Node";
  213.     nodes[2].ln_Name = "Third Node";
  214.     nodes[3].ln_Name = "Fourth Node";
  215.     nodes[4].ln_Name = "Fifth Node";
  216.     NewList(&list);
  217.     AddTail(&list,&nodes[0]);
  218.     AddTail(&list,&nodes[1]);
  219.     AddTail(&list,&nodes[2]);
  220.     AddTail(&list,&nodes[3]);
  221.     AddTail(&list,&nodes[4]);
  222.  
  223.     /* open all basic libraries in case we need 'em later */
  224.     if (IntuitionBase = OpenLibrary("intuition.library", 39))
  225.     {
  226.         GfxBase       = OpenLibrary("graphics.library", 39);
  227.         GadToolsBase  = OpenLibrary("gadtools.library", 39);
  228.         UtilityBase   = OpenLibrary("utility.library", 39);
  229.     }
  230.  
  231.     /* check if we got everything we need */
  232.     if (IntuitionBase && GfxBase && GadToolsBase && UtilityBase)
  233.     {
  234.         if (screen = LockPubScreen(NULL))
  235.         {
  236.             if (drawInfo = GetScreenDrawInfo(screen))
  237.             {
  238.                 if (visualInfo = GetVisualInfoA(screen,NULL))
  239.                 {
  240.                     gadgetList = NULL;
  241.  
  242.                     ng.ng_LeftEdge   = 10;
  243.                     ng.ng_TopEdge    = screen->WBorTop + screen->Font->ta_YSize + 1 + 16;
  244.                     ng.ng_Width      = 200;
  245.                     ng.ng_Height     = 120;
  246.                     ng.ng_GadgetText = "A List Of Things";
  247.                     ng.ng_TextAttr   = &topazAttr;
  248.                     ng.ng_Flags      = 0;
  249.                     ng.ng_VisualInfo = visualInfo;
  250.  
  251.                     if (CreateGadget(LISTVIEW_KIND,CreateContext(&gadgetList),&ng,
  252.                                      GTLV_Labels,       &list,
  253.                                      GTLV_ShowSelected, NULL,
  254.                                      GTLV_ScrollWidth,  19,
  255.                                      GTLV_ItemHeight,   12,
  256.                                      GTLV_CallBack,     &renderHook,
  257.                                      GTLV_Selected,     0,
  258.                                      GTLV_MaxPen,       255,
  259.                                      TAG_DONE))
  260.                     {
  261.                         if (window = OpenWindowTags(NULL,
  262.                                           WA_InnerWidth,   220,
  263.                                           WA_InnerHeight,  150,
  264.                                           WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | LISTVIEWIDCMP,
  265.                                           WA_DepthGadget,  TRUE,
  266.                                           WA_DragBar,      TRUE,
  267.                                           WA_CloseGadget,  TRUE,
  268.                                           WA_SimpleRefresh,TRUE,
  269.                                           WA_Activate,     TRUE,
  270.                                           WA_Title,        "ListView Tester",
  271.                                           WA_PubScreen,    screen,
  272.                                           WA_AutoAdjust,   TRUE,
  273.                                           WA_Gadgets,      gadgetList,
  274.                                           TAG_DONE))
  275.                         {
  276.                             GT_RefreshWindow(window,NULL);
  277.  
  278.                             quit = FALSE;
  279.                             while (!quit)
  280.                             {
  281.                                 WaitPort(window->UserPort);
  282.                                 if (intuiMsg = (struct IntuiMessage *)GT_GetIMsg(window->UserPort))
  283.                                 {
  284.                                     if (intuiMsg->Class == IDCMP_REFRESHWINDOW)
  285.                                     {
  286.                                         GT_BeginRefresh(window);
  287.                                         GT_EndRefresh(window,TRUE);
  288.                                     }
  289.                                     else if (intuiMsg->Class == IDCMP_CLOSEWINDOW)
  290.                                     {
  291.                                         quit = TRUE;
  292.                                     }
  293.                                     GT_ReplyIMsg(intuiMsg);
  294.                                 }
  295.                             }
  296.  
  297.                             CloseWindow(window);
  298.                         }
  299.                     }
  300.                     FreeGadgets(gadgetList);
  301.                     FreeVisualInfo(visualInfo);
  302.                 }
  303.                 FreeScreenDrawInfo(screen,drawInfo);
  304.             }
  305.             UnlockPubScreen(NULL,screen);
  306.         }
  307.     }
  308.  
  309.     if (IntuitionBase)
  310.     {
  311.         CloseLibrary(UtilityBase);
  312.         CloseLibrary(GadToolsBase);
  313.         CloseLibrary(GfxBase);
  314.         CloseLibrary(IntuitionBase);
  315.     }
  316.  
  317.     return(0);
  318. }
  319.